package com.gansu.weather.widget;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.gansu.weather.bean.Weather;
import com.gansu.weather.util.DateUtils;
import com.gansu.weather.view.WeatherActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.text.format.Time;
import android.widget.RemoteViews;

public class NewsService extends Service implements Runnable {  
    
    private static final String TAG="NewsService";   
  
    private static Queue<Integer> sAppWidgetIds=new LinkedList<Integer>();  
      
    public static final String ACTION_UPDATE_ALL = "com.xxxx.news.UPDATE_ALL";  
      
    private static boolean sThreadRunning = false;  
      
    private static Object sLock = new Object();  
      
  
      
    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
      
    public static void updateAppWidgetIds(int[] appWidgetIds){  
        synchronized (sLock) {  
            for (int appWidgetId : appWidgetIds) {  
                sAppWidgetIds.add(appWidgetId);  
            }  
        }  
    }  
      
    public static int getNextWidgetId(){  
        synchronized (sLock) {  
            if (sAppWidgetIds.peek() == null) {  
                return AppWidgetManager.INVALID_APPWIDGET_ID;  
            } else {  
                return sAppWidgetIds.poll();  
                  
            }  
        }  
    }  
      
    private static boolean hasMoreUpdates() {  
        synchronized (sLock) {  
            boolean hasMore = !sAppWidgetIds.isEmpty();  
            if (!hasMore) {  
                sThreadRunning = false;  
            }  
            return hasMore;  
        }  
    }  
  
    @Override  
    public void onCreate() {  
        super.onCreate();  
    }  
  
    @Override  
    public void onStart(Intent intent, int startId) {  
        super.onStart(intent, startId);  
        if (null != intent) {  
            if (ACTION_UPDATE_ALL.equals(intent.getAction())) {  
                AppWidgetManager widget = AppWidgetManager.getInstance(this);  
                updateAppWidgetIds(widget.getAppWidgetIds(new ComponentName(this, Widget.class)));  
            }  
        }  
        synchronized (sLock) {  
            if (!sThreadRunning) {  
                sThreadRunning=true;  
                new Thread(this).start();  
            }  
        }  
    }  
  
    @Override  
    public void run() {  
          
        AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this);  
        RemoteViews updateViews=null;  
          
        while (hasMoreUpdates()) {  
            int appWidgetId=getNextWidgetId();  
            String time = DateUtils.getCurrTime();
            String date = DateUtils.getCurrDate();
            updateViews = Widget.updateAppWidget(this, time,date);  
            if (updateViews != null) {  
                appWidgetManager.updateAppWidget(appWidgetId, updateViews);  
            }  
        }  
          
        Intent updateIntent=new Intent(ACTION_UPDATE_ALL);  
        updateIntent.setClass(this, NewsService.class);  
        PendingIntent pending=PendingIntent.getService(this, 0, updateIntent, 0);  
          
        AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);  
        alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pending);  
        stopSelf();  
    }  
  
}  